package com.huanxing.cloud.common.security.feign;

import com.huanxing.cloud.common.core.constant.SecurityConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

/** feign 请求拦截 @Author lijx @Date 2022/1/27 13:29 */
public class OAuth2FeignRequestInterceptor implements RequestInterceptor {
  @Override
  public void apply(RequestTemplate requestTemplate) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails)
    {
      OAuth2AuthenticationDetails dateils = (OAuth2AuthenticationDetails) authentication.getDetails();
      //设置请求token信息
      requestTemplate.header(HttpHeaders.AUTHORIZATION,
              String.format("%s %s", SecurityConstants.BEARER_TOKEN_TYPE, dateils.getTokenValue()));
    }
  }
}
